package io.github.talelin.latticy.controller.v1.K;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.K.ViewCollectorDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

/**
 * VIEW前端控制器
 *
* @author generator@TaleLin
* @since 2021-07-10
*/
@RestController
@RequestMapping("/v1/view-collector")
public class ViewCollectorController {

    @Autowired
    private io.github.talelin.latticy.service.K.ViewCollectorService ViewCollectorService;
    
    @PostMapping("")
    public CreatedVO create() {
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public ViewCollectorDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return null;
    }

    @GetMapping("/page")
    public PageResponseVO<ViewCollectorDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "StartTime1") String StartTime1,
            @RequestParam(name = "EndTime1") String EndTime1,
            @RequestParam(name = "StartTime2") String StartTime2,
            @RequestParam(name = "EndTime2") String EndTime2,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4,

            @RequestParam(name = "collector_state") String collector_state,
            @RequestParam(name = "group_id") String group_id,
            @RequestParam(name = "collector_state1") String collector_state1,
            @RequestParam(name = "group_id1") String group_id1,
            @RequestParam(name = "collector_state2") String collector_state2,
            @RequestParam(name = "group_id2") String group_id2,
            @RequestParam(name = "collector_state3") String collector_state3,
            @RequestParam(name = "group_id3") String group_id3,
            @RequestParam(name = "collector_state4") String collector_state4,
            @RequestParam(name = "group_id4") String group_id4
    ) {
        Page<ViewCollectorDO> pager = new Page<>(page, count);
        QueryWrapper<ViewCollectorDO> wrapper = new QueryWrapper<>();
        if(StartTime1 != ""){
            wrapper.ge("using_date",StartTime1);
        }
        if(EndTime1 != ""){
            wrapper.le("using_date",EndTime1);
        }
        if(StartTime2 != ""){
            wrapper.ge("buy_date",StartTime2);
        }
        if(EndTime2 != ""){
            wrapper.le("buy_date",EndTime2);
        }
        if(dwmc != "") {
            wrapper.and(i -> i.eq("dwmc", dwmc)
                    .or().eq("dwmc", dwmc1)
                    .or().eq("dwmc", dwmc2)
                    .or().eq("dwmc", dwmc3)
                    .or().eq("dwmc", dwmc4));
        }
        if(bgateway_name != "") {
            wrapper.and(i -> i.eq("b_gateway_name", bgateway_name)
                    .or().eq("b_gateway_name", bgateway_name1)
                    .or().eq("b_gateway_name", bgateway_name2)
                    .or().eq("b_gateway_name", bgateway_name3)
                    .or().eq("b_gateway_name", bgateway_name4));
        }
        if(collector_name != "") {
            wrapper.and(i -> i.eq("collector_name", collector_name)
                    .or().eq("collector_name", collector_name1)
                    .or().eq("collector_name", collector_name2)
                    .or().eq("collector_name", collector_name3)
                    .or().eq("collector_name", collector_name4));
        }
        if(group_id != "") {
            wrapper.and(i -> i.eq("group_id", group_id)
                    .or().eq("group_id", group_id1)
                    .or().eq("group_id", group_id2)
                    .or().eq("group_id", group_id3)
                    .or().eq("group_id", group_id4));
        }
        if(collector_state != "") {
            wrapper.and(i -> i.eq("collector_state", collector_state)
                    .or().eq("collector_state", collector_state1)
                    .or().eq("collector_state", collector_state2)
                    .or().eq("collector_state", collector_state3)
                    .or().eq("collector_state", collector_state4));
        }
        IPage<ViewCollectorDO> paging = ViewCollectorService.getBaseMapper().selectPage(pager, wrapper);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<ViewCollectorDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword,
            @RequestParam(name = "StartTime1") String StartTime1,
            @RequestParam(name = "EndTime1") String EndTime1,
            @RequestParam(name = "StartTime2") String StartTime2,
            @RequestParam(name = "EndTime2") String EndTime2,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4,

            @RequestParam(name = "collector_state") String collector_state,
            @RequestParam(name = "group_id") String group_id,
            @RequestParam(name = "collector_state1") String collector_state1,
            @RequestParam(name = "group_id1") String group_id1,
            @RequestParam(name = "collector_state2") String collector_state2,
            @RequestParam(name = "group_id2") String group_id2,
            @RequestParam(name = "collector_state3") String collector_state3,
            @RequestParam(name = "group_id3") String group_id3,
            @RequestParam(name = "collector_state4") String collector_state4,
            @RequestParam(name = "group_id4") String group_id4
    ) {
        Page<ViewCollectorDO> pager = new Page<>(page, count);
        IPage<ViewCollectorDO> paging = ViewCollectorService.seachViewCollector(pager,keyword,StartTime1,EndTime1,StartTime2,EndTime2,dwmc,bgateway_name,collector_name,dwmc1,
                bgateway_name1,
                collector_name1,
                dwmc2,
                bgateway_name2,
                collector_name2,
                dwmc3,
                bgateway_name3,
                collector_name3,
                dwmc4,
                bgateway_name4,
                collector_name4,
                collector_state,
                group_id,
                collector_state1,
                group_id1,
                collector_state2,
                group_id2,
                collector_state3,
                group_id3,
                collector_state4,
                group_id4
        );
        return PageUtil.build(paging);
    }

}
